package cn.doitedu.operate

import org.apache.log4j.{Level, Logger}
import org.apache.spark.sql.{DataFrame, SparkSession}

/**
 * @Date 22.4.10
 * @Created by HANGGE
 * @Description
 */
object C06_DSL_Sub_Select {
  Logger.getLogger("org").setLevel(Level.ERROR)
  def main(args: Array[String]): Unit = {
    // 1 获取会话
    val session = SparkSession.builder()
      .appName(this.getClass.getSimpleName)
      .master("local[*]")
      .getOrCreate()

    import  session.implicits._
    import  org.apache.spark.sql.functions._
     // 导入SQL函数和隐式
    // 具有丰富的操作字段的函数
    // 2 加载数据   创建DataFrame
    val df: DataFrame = session.read.option("header" , true).option("inferSchema",true).csv("file:///D://code/doit30_spark_sql/data/csv/Teacher2.csv")

    /**
     * select
     * from
     * (
     * select
     * from
     * tb_teacher
     * where sal > 20000
     * ) x
     * where  gender = "M"
     *
     */

 /*   df.where("sal > 20000")
      .select("id" , "name" , "age" , "sal")
      .where("gender = 'M'")*/

    df.groupBy("gender")
      .agg(sum("sal") as "sal")
      .where("sal > 30000")
      .select("gender" , "sal")
      .show()
  }
}
